home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / irit40s.lha / Irit / cagd_lib / cbspeval.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-30  |  4.1 KB  |  128 lines

  1. /******************************************************************************
  2. * CBspEval.c - Bezier curves handling routines - evaluation routines.          *
  3. *******************************************************************************
  4. * Written by Gershon Elber, Mar. 90.                          *
  5. ******************************************************************************/
  6.  
  7. #include <ctype.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "cagd_loc.h"
  11.  
  12. /******************************************************************************
  13. * Assumes Vec holds control points for scalar bspline curve of order Order    *
  14. * length Len and knot vector KnotVector.                      *
  15. * Evaluates and returns that curve value at parameter value t.              *
  16. * Vec is incremented by VecInc (usually by 1) after each iteration.          *
  17. ******************************************************************************/
  18. CagdRType BspCrvEvalVecAtParam(CagdRType *Vec, int VecInc,
  19.                    CagdRType *KnotVector, int Order, int Len,
  20.                    CagdRType t)
  21. {
  22.     int i, IndexFirst;
  23.     CagdRType
  24.     R = 0.0,
  25.     *BasisFunc = BspCrvCoxDeBoorBasis(KnotVector, Order, Len, t,
  26.                                 &IndexFirst);
  27.  
  28.     if (VecInc == 1) {
  29.     Vec += IndexFirst;
  30.     for (i = 0; i < Order; i++)
  31.         R += BasisFunc[i] * *Vec++;
  32.     }
  33.     else {
  34.     Vec += IndexFirst * VecInc;
  35.     for (i = 0; i < Order; i++) {
  36.         R += BasisFunc[i]  * *Vec;
  37.         Vec += VecInc;
  38.     }
  39.     }
  40.  
  41.     return R;
  42. }
  43.  
  44. /******************************************************************************
  45. * Returns a pointer to a static data, holding the value of the curve at given *
  46. * parametric location t. The curve is assumed to be Bspline.              *
  47. * Uses the Cox de Boor recursive algorithm (is this fastest for single eval?) *
  48. ******************************************************************************/
  49. CagdRType *BspCrvEvalAtParam(CagdCrvStruct *Crv, CagdRType t)
  50. {
  51.     return BspCrvEvalCoxDeBoor(Crv, t);
  52. }
  53.  
  54. /******************************************************************************
  55. * Samples the curves at FineNess location equally spaced in the Bspline       *
  56. * parametric domain.                                  *
  57. *   Computes a refinement alpha matrix (If FineNess > 0), refine the curve    *
  58. * and use refined control polygon as the approximation to the curve.          *
  59. *   If FineNess == 0, Alpha matrix A is used instead.                  *
  60. *   Returns the actual number of points in polyline (<= FineNess).          *
  61. * Note bezier curves may call this routine.                      *
  62. ******************************************************************************/
  63. int CagdCrvEvalToPolyline(CagdCrvStruct *Crv, int FineNess,
  64.               CagdRType *Points[], BspKnotAlphaCoeffType *A)
  65. {
  66.     CagdBType
  67.     IsNotRational = !CAGD_IS_RATIONAL_CRV(Crv);
  68.     int i, j, Count,
  69.     Len = Crv -> Length,
  70.         n = FineNess == 0 ? A -> RefLength : (1 << FineNess),
  71.     Order = Crv -> Order,
  72.     MaxCoord = CAGD_NUM_OF_PT_COORD(Crv -> PType);
  73.     CagdRType Tmin, Tmax;
  74.  
  75.     if (Crv -> Order == 2) {         /* Simply copy the control polygon. */
  76.     CagdRType **CrvPoints = Crv -> Points;
  77.  
  78.     for (Count = 0; Count < n && Count < Len; Count++)
  79.         for (i = IsNotRational; i <= MaxCoord; i++)
  80.             Points[i][Count] = CrvPoints[i][Count];
  81.     return Count;
  82.     }
  83.  
  84.     if (CAGD_IS_BEZIER_CRV(Crv)) {
  85.     Tmin = 0.0;
  86.     Tmax = 1.0;
  87.     }
  88.     else
  89.     BspCrvDomain(Crv, &Tmin, &Tmax);
  90.  
  91.     if (FineNess > 0) {
  92.     CagdRType *RefKV;
  93.  
  94.     if (n <= Len)
  95.         FATAL_ERROR(CAGD_ERR_REF_LESS_ORIG);
  96.  
  97.     RefKV = BspKnotPrepEquallySpaced(n - Len, Tmin, Tmax);
  98.  
  99.     if (CAGD_IS_BEZIER_CRV(Crv)) {
  100.         CagdRType
  101.         *KV = BspKnotUniformOpen(Crv -> Length, Crv -> Order, NULL);
  102.  
  103.         A = BspKnotEvalAlphaCoefMerge(Order, KV, Len, RefKV, n - Len);
  104.  
  105.         IritFree((VoidPtr) KV);
  106.     }
  107.     else
  108.         A = BspKnotEvalAlphaCoefMerge(Order, Crv -> KnotVector, Len,
  109.                       RefKV, n - Len);
  110.  
  111.     IritFree((VoidPtr) RefKV);
  112.     }
  113.  
  114.     /* Compute the refined control polygon straight to destination Points. */
  115.     for (j = IsNotRational; j <= MaxCoord; j++) {
  116.     CagdRType
  117.         *ROnePts = Points[j],
  118.         *OnePts = Crv -> Points[j];
  119.     for (i = 0; i < n; i++, ROnePts++)
  120.         CAGD_ALPHA_BLEND(A, i, OnePts, ROnePts);
  121.     }
  122.  
  123.     if (FineNess > 0)
  124.     BspKnotFreeAlphaCoef(A);
  125.  
  126.     return n;
  127. }
  128.